package com.v5ba.annotation.async.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author yiren
 * @since 2022/2/5
 */
@Configuration
public class MyAsyncConfiguration {
	//核心线程数
	private static final int CORE_POOL_SIZE = 5;
	//最大线程数
	private static final int MAX_POOL_SIZE = 15;
	//队列大小
	private static final int QUEUE_CAPACITY = 100;
	//线程池中的线程的名称前缀
	private static final String THREAD_NAME = "MyThread-";
	@Bean
	public Executor getAsyncExecutor1(){
		return Executors.newFixedThreadPool(4);
	}
	//@Bean
	public Executor getAsyncExecutor3() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//配置核心线程数
		executor.setCorePoolSize(CORE_POOL_SIZE);
		//配置最大线程数
		executor.setMaxPoolSize(MAX_POOL_SIZE);
		//配置队列大小
		executor.setQueueCapacity(QUEUE_CAPACITY);
		//配置线程池中的线程的名称前缀
		executor.setThreadNamePrefix(THREAD_NAME);
		//配置线程池拒绝策略，设置为CallerRunsPolicy，当线程和队列都满了，由发起线程的主线程自己执行
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.initialize();
		return executor;
	}

	@Bean
	public Executor getExecutor(){
		return Executors.newFixedThreadPool(4);
	}

	//@Bean
	public Executor getAsyncExecutor2() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//配置核心线程数
		executor.setCorePoolSize(CORE_POOL_SIZE);
		//配置最大线程数
		executor.setMaxPoolSize(MAX_POOL_SIZE);
		//配置队列大小
		executor.setQueueCapacity(QUEUE_CAPACITY);
		//配置线程池中的线程的名称前缀
		executor.setThreadNamePrefix(THREAD_NAME);
		//配置线程池拒绝策略，设置为CallerRunsPolicy，当线程和队列都满了，由发起线程的主线程自己执行
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.initialize();
		return executor;
	}
}
